const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actions: [
      { text: "技师" , id: 1},
    ],
    actions_name: "需求",
    popover_show: false,
    name: "",
    myLocation: {},
    dataList: [],
    page: 1,
    finished: false,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      myLocation: wx.getStorageSync("address-point"),
      actions_name: options && options.name ? options.name : '需求'
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onSelect(item) {
    let actions = [{ text: item.currentTarget.dataset.name ==  '需求' ? "技师" : "需求", id: item.currentTarget.dataset.name ==  '需求' ? 1 : 2}]
    this.setData({
      actions,
      actions_name: item.currentTarget.dataset.name,
      popover_show: false,
      dataList: [],
      page: 1,
      finished:false
    })
    this.getData();
  },
  btnPopoverShow() {
    let actions = [{ text: this.data.actions_name ==  '需求' ? "技师" : "需求", id: this.data.actions_name ==  '需求' ? 1 : 2}];
    this.setData({
      actions,
      popover_show: true
    })
  },
  getData () {
    if (this.data.finished) return;
    let url = this.data.actions_name == '需求' ? 'plugin.be-within-call.frontend.public-search.need-order' : 'plugin.be-within-call.frontend.public-search.shifu'
    let baseUrl = app.getNetAddresss(url);
    app._postNetWork({
      url: baseUrl,
      data: {
        lng: this.data.myLocation ? this.data.myLocation.point.lng : "",
        lat: this.data.myLocation ? this.data.myLocation.point.lat : "",
        name: this.data.name,
        page_size: 15,
        page: this.data.page
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          app.tips(respone.data.msg)
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let dataList = [];
        if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
          finished = true;
        }
        dataList = this.data.dataList.concat(res.data);

        this.setData({
          dataList,
          page: ++this.data.page,
          finished
        });
      }
    });
  },
  goDetailPage(ev) {
    if(ev.currentTarget.dataset.type == '需求') {
      wx.navigateTo({
        url:  `/packageJ/anytime_service/home/individual_needs/index?type=1&order_id=${ev.currentTarget.dataset.id}`,
      });
    }else if(ev.currentTarget.dataset.type == '技师') {
      wx.navigateTo({
        url:  `/packageJ/anytime_service/home/technician_home/index?id=${ev.currentTarget.dataset.id}`,
      });
    }
  },
  onChange(e) {
    this.setData({
      name: e.detail,
    });
  },
  inputValue(e) {
    this.setData({
      dataList: [],
      page: 1,
      name: e.type == "clear" ? "" : this.data.name,
      finished: false
    })
    this.getData();
  },
})